package fausto.cycles 
{
	import com.greensock.TweenMax;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Ben.
	 */
	public class CyclesContent extends Sprite
	{
		private var _i:Infos
		private var _d:XML
		private var _ci:CyclesItem
		private var _ct:CyclesThumb
		private var _cf:CyclesFull
		private var _t:String
		
		private var _tempo:Number = 0.6;
		
		// SPRITE VIDES
		public var _spItems:Sprite
		public var _spThumbs:Sprite
		public var _spFull:Sprite
		
		// BACK BTN
		public var _backStep1Btn:Sprite
		public var _backStep2Btn:Sprite
		
		//public var _titleTxt:TextField
		
		public function CyclesContent() 
		{
			visible = false;
			
			tabCycles = new Array();
			
			_i = Infos.getInstance();
			
			_spThumbs.alpha = 0;
			_spFull.alpha = 0;
			
			_backStep1Btn.visible = false;
			_backStep2Btn.visible = false;
			
			// ITEMS TO THUMBS
			addEventListener("CYCLES_ITEM_THUMB", itemsToThumbs);
			// THUMBS TO FULL
			addEventListener("CYCLES_THUMB_FULL", thumbsToFull);
			// FULL TO THUMBS
			_backStep2Btn.addEventListener(MouseEvent.CLICK, fullToThumbs);
			// THUMBS TO ITEMS
			_backStep1Btn.addEventListener(MouseEvent.CLICK, thumbsToItems);
			
		}
		
		private function itemsToThumbs(e:Event):void 
		{
			TweenMax.to(_spItems, _tempo, { alpha:0, x: -640, onComplete:disabledIt, onCompleteParams:[_spItems] } );
			// CREATE CYCLE
			createThumbs(e.target._id);
			TweenMax.to(_spThumbs, _tempo, {alpha:1, x:0, delay:0.3, onStart:enabledIt, onStartParams:[_spThumbs]  } );
			TweenMax.to(_spFull, 0, { x:640 } );
			_backStep1Btn.visible = true;
		}
		
		private function thumbsToFull(e:Event):void 
		{
			_backStep1Btn.visible = false;
			_backStep2Btn.visible = true;
			TweenMax.to(_spThumbs, _tempo, { alpha:0, x: -640, onComplete:disabledIt, onCompleteParams:[_spThumbs] } );
			// CREATE FULL
			createFull(e.target._id);
			TweenMax.to(_spFull, _tempo, {alpha:1, x:0, delay:0.3, onStart:enabledIt, onStartParams:[_spFull] } );
		}
				
		private function fullToThumbs(e:Event):void 
		{
			_backStep2Btn.visible = false;
			_backStep1Btn.visible = true;
			TweenMax.to(_spFull, _tempo, { alpha:0, x: 640, onComplete:disabledIt, onCompleteParams:[_spFull] } );
			TweenMax.to(_spThumbs, _tempo, {alpha:1, x:0, delay:0.3, onStart:enabledIt, onStartParams:[_spThumbs] } );
		}
		
		private function thumbsToItems(e:Event):void 
		{
			_backStep1Btn.visible = false;
			TweenMax.to(_spThumbs, _tempo, { alpha:0, x: 640, onComplete:disabledIt, onCompleteParams:[_spThumbs] } );
			TweenMax.to(_spItems, _tempo, {alpha:1, x:0, delay:0.3, onStart:enabledIt, onStartParams:[_spItems]  } );
		}
		
		private var _nbThumbs:int
		private var _currentThumb:int
		private var _currentCycle:int
		private var _yThumb:int
		private var _xThumb:int
		
		private function createFull(_n:int):void 
		{
			// CLEAN OLD FULL
			if (_cf != null)
			{
				trace("CLEAN OLD");
				_cf.killBill();
				_spFull.removeChild(_cf);
				_cf = null;
			}
			_cf = new CyclesFull(_d.cycle[_currentCycle], _n);
			_cf.y = 20;
			_spFull.addChild(_cf);
		}
		
		private function createThumbs(_n:int):void 
		{
			var yInit:int = 20;
			var xInit:int = 0;
			
			// CLEAN OLD CYCLES
			cleanCycles();
			
			_nbThumbs = _d.cycle[_n].photo.length();
			//_titleTxt.htmlText = "./ " + _d.cycle[_n].label;
			_currentThumb = 0;
			
			_currentCycle = _n;
			
			trace("current cycle "+_currentCycle);
			
			_yThumb = 20;
			_xThumb = 0;
			
			loadThumb();
		}
		
		private var tabCycles:Array
		
		private function cleanCycles():void 
		{
			if (tabCycles.length != 0)
			{
				for (var i:int = 0; i < tabCycles.length; i++)
				{
					_ct = tabCycles[i] as CyclesThumb;
					_spThumbs.removeChild(_ct);
				}
			}
			tabCycles = new Array();
		}
		
		private function loadThumb(e:Event = null):void 
		{
			if (_currentThumb == _nbThumbs)
			{
				return;
			}
			
			if (_currentThumb % 4 == 0 && _currentThumb != 0)
			{
				_yThumb += 160;
				_xThumb = 0;
			}
			
			_ct = new CyclesThumb(_d.cycle[_currentCycle].photo[_currentThumb], _currentThumb);
			_ct.addEventListener("THUMB_LOADED", loadThumb);
			_ct.y = _yThumb;
			_ct.x = _xThumb;
			
			tabCycles.push(_ct);
			
			_xThumb += 160;
			_spThumbs.addChild(_ct);
			
			_currentThumb++;
		}
		
		public function createItems():void
		{
			_d = _i.theXML.part[1];
			
			_t = _d.label;
			
			//_titleTxt.htmlText = "./ " + _t;
			
			var yInit:int = 20;
			var xInit:int = 0;
			
			for (var i:int = 0; i < _d.cycle.length(); i++)
			{
				if (i % 4 == 0 && i != 0)
				{
					yInit += 160;
					xInit = 0;
				}
				
				_ci = new CyclesItem(_d.cycle[i].label, i);
				_ci.y = yInit;
				_ci.x = xInit;
				
				xInit += 160;
				
				_spItems.addChild(_ci);	
			}
		}
		
		private function enabledIt(sp:Sprite):void
		{
			sp.mouseEnabled = true;
			sp.visible = true;
		}
		
		private function disabledIt(sp:Sprite):void
		{
			sp.mouseEnabled = false;
			sp.visible = false;
		}
		
	}

}